

public class AIDirector extends Thread {

  int numberOfZombies = 25;
  int numberOfZombiesLeftToSpawn = 125;
  int currentPhase;
  ArrayList normalZombies = new ArrayList();
  ArrayList deadZombies = new ArrayList();
  boolean isRunning = false;
  PImage zombieImages[] = {loadImage("zombie.png")};
  PImage zombieAttack[] = {loadImage("zombie.png"), loadImage("zombiehitting1.png")};
  PImage zombieMoving[] = {loadImage("zombiewalking1.png"), loadImage("zombiewalking2.png"), loadImage("zombie.png"), loadImage("zombiewalking3.png"), loadImage("zombiewalking4.png"), loadImage("zombiewalking3.png"), loadImage("zombie.png"), loadImage("zombiewalking2.png")};
  boolean started = false;
  PVector center = new PVector(width/2.0, height/2.0);
  
 public AIDirector(){
   PVector startingLocation = new PVector(0, 0);
   int mod;
   for(int i = 0; i < numberOfZombies; i++){
     numberOfZombiesLeftToSpawn--;
     mod = i%4;
     if(mod == 0){
       startingLocation.x = -200;
       startingLocation.y = random(0, height);
     } else if (mod == 1){
       startingLocation.x = width + 200;
       startingLocation.y = random(0, height);       
     }  else if (mod == 2){
       startingLocation.x = random(0, width);
       startingLocation.y = -200;       
     } else {
       startingLocation.x = random(0, width);;
       startingLocation.y = height + 200;       
     }
     normalZombies.add(i, new Zombie(zombieImages, zombieMoving, zombieAttack, 200, 35, startingLocation, 50.0));
    }
    this.updateVelocity();
 }
  
 public void setCurrentPhase(int currentPhase){
  this.currentPhase = currentPhase; 
 }
 
 public boolean isStarted(){
  return started; 
 }
 
 public void setStarted(){
  started = true; 
 }
 
 public boolean isRunning(){
   return isRunning;
 }
  
 public void setRunning(){
  isRunning = true; 
 }
 
 public void checkIfHit(float currentAngle, int attackRadius, PVector ninjasLocation){
  Zombie aZombie;
  float xDistance, yDistance, startingAngle = currentAngle + PI/3.0, endingAngle =  currentAngle - PI/3.0, currentRadius, mod;
   PVector startingLocation = new PVector(0, 0);
  for(int i = 0; i < normalZombies.size(); i++){
     aZombie = ((Zombie)(normalZombies.get(i)));
     currentRadius = aZombie.currentLocation.dist(ninjasLocation);
     if(currentRadius < attackRadius){
        xDistance = aZombie.currentLocation.x - ninjasLocation.x;
        yDistance = aZombie.currentLocation.y - ninjasLocation.y;
        if((xDistance < 0) && (yDistance > 0)){
          currentAngle = PI/2 + acos(xDistance/currentRadius);
        } 
        else if ((xDistance < 0) && (yDistance < 0)){
          currentAngle = 5*PI/2 - acos(xDistance/currentRadius);
        } 
        else if ((xDistance > 0) && (yDistance < 0)) {
          currentAngle = PI/2 - acos(xDistance/currentRadius);
        } 
        else if((xDistance > 0) && (yDistance > 0)) {
          currentAngle = PI/2 + acos(xDistance/currentRadius);
        }
        if((currentAngle < startingAngle)  && (currentAngle > endingAngle)){
          if(i % 3 == 0){        
            deadZombies.add(deadZombies.size(), new Image(loadImage("deadzombie1.png"), new PVector(aZombie.currentLocation.x, aZombie.currentLocation.y), 400, 0, .50));
          } else if (i % 3 == 1) {
            deadZombies.add(deadZombies.size(), new Image(loadImage("deadzombie2.png"), new PVector(aZombie.currentLocation.x, aZombie.currentLocation.y), 400, 0, .50));
          } else {
            deadZombies.add(deadZombies.size(), new Image(loadImage("deadzombie3.png"), new PVector(aZombie.currentLocation.x, aZombie.currentLocation.y), 400, 0, .50));
          }
          numberOfZombiesLeftToSpawn--;
          if(numberOfZombiesLeftToSpawn > 0){
            mod = i%4;
           if(mod == 0){
             startingLocation.x = -200;
             startingLocation.y = random(0, height);
           } else if (mod == 1){
             startingLocation.x = width + 200;
             startingLocation.y = random(0, height);       
           }  else if (mod == 2){
             startingLocation.x = random(0, width);
             startingLocation.y = -200;       
           } else {
             startingLocation.x = random(0, width);;
             startingLocation.y = height + 200;       
           }
            ((Zombie)(normalZombies.get(i))).setLocation(startingLocation);
            ((Zombie)(normalZombies.get(i))).setVelocity(center);
          } else {
           normalZombies.remove(i); 
          }
        }
     }
   }
 }
  
 private void updateVelocity(){Zombie aZombie;
  for(int i = 0; i < normalZombies.size(); i++){
     aZombie = ((Zombie)(normalZombies.get(i)));
     aZombie.setVelocity(center);
   }
 } 
  
 public void displayDeadZombies(int currentTime){
  Image aDeadZombie;
   for(int i = 0; i < deadZombies.size(); i++){
     aDeadZombie = ((Image)(deadZombies.get(i)));
     aDeadZombie.drawImageWithFadeOutTint(currentTime);
     if(aDeadZombie.hasTimeExpired() == true){
       deadZombies.remove(i); 
     }
   }
 }
  
 public void displayZombies(int currentTime){
  Zombie aZombie;
  for(int i = 0; i < normalZombies.size(); i++){
     aZombie = ((Zombie)(normalZombies.get(i)));
     if(aZombie.isAttacking() == true){
       aZombie.attack(currentTime);
     } else {
       aZombie.moveToAreaThroughAnimation();
     }
   }
 } 
 
 public void run(){
   isRunning = true;
   int currentTime = millis();
   Zombie aZombie;
   for(int i = 0; i < normalZombies.size(); i++){
     aZombie = ((Zombie)(normalZombies.get(i)));
     aZombie.updatePosition(currentTime);
   }
   isRunning = false;
   
 }
  
}



